var CsOrderPcoRecordsDatagrid = new HgUi.Datagrid("CsOrderPcoRecords_datagrid");
CsOrderPcoRecordsDatagrid.toolbar = $("#CsOrderPcoRecords_toolbar");
CsOrderPcoRecordsDatagrid.mode = "rowEdit"; //行编辑模式
CsOrderPcoRecordsDatagrid.options.isShowContextMenu = true;
CsOrderPcoRecordsDatagrid.options.onSelect = function(index,row) {
    if (row && row.dictGroupId){
        CsOrderPcoRecordsDatagrid.selectedRow = row;
    }
};
CsOrderPcoRecordsDatagrid.options.onLoadSuccess = function(data) {
    CsOrderPcoRecordsDatagrid.grid.datagrid("selectRow",0);
};
CsOrderPcoRecordsDatagrid.init();
CsOrderPcoRecordsDatagrid.grid.datagrid("enableFilter");
CsOrderPcoRecordsDatagrid.bindDefaultToolbarEvent();

// --------------------------保存数据------------------------------------------------------------------
CsOrderPcoRecordsDatagrid.toolbar.find("[tag=save]").click(function() {
    if (CsOrderPcoRecordsDatagrid.isEditing == true) {
        if (CsOrderPcoRecordsDatagrid.grid.datagrid("validateRow", CsOrderPcoRecordsDatagrid.editingIndex)) {
            var data = CsOrderPcoRecordsDatagrid.makeSubmitData();
            var editRow = CsOrderPcoRecordsDatagrid.grid.datagrid("getRows")[CsOrderPcoRecordsDatagrid.editingIndex];
            var submitUrl = "/cs/CsOrderPcoRecords/add";
            if (editRow.id) {
                submitUrl = "/cs/CsOrderPcoRecords/upd";
                data.id = editRow.id;
            }
            HgUtil.post(submitUrl, data, function(data) {
                if (!data.success) {
                    CsOrderPcoRecordsDatagrid.showMsgbox("error", data.data, 330);
                } else {
                    CsOrderPcoRecordsDatagrid.grid.datagrid('reload');
                }
            });

        } else {
            CsOrderPcoRecordsDatagrid.showMsgbox("alert", "有一条数据未校验通过!", 180);
        }
    }
});

//-------------------------删除-----------------------------------------------------------------------
CsOrderPcoRecordsDatagrid.toolbar.find("[tag=del]").click(function(){
    var row = CsOrderPcoRecordsDatagrid.grid.datagrid("getSelected");
    if (!row) {
        $.messager.alert("提示","请选择一条字典组数据","warning");
        return;
    }
    $.messager.confirm("确认删除", "确认将选中的字典组删除?", function(r){
        if (r) {
            HgUtil.getJson("/cs/CsOrderPcoRecords/del",{id:row.id}, function(data){
                if (!data.success) {
                    $.messager.alert("提示","删除失败,"+data.data,"warning");
                } else {
                    HgUi.ok("删除成功!",function(){
                        CsOrderPcoRecordsDatagrid.grid.datagrid("reload");
                    });
                }
            });
        }
    });
});

// 组织提交数据
CsOrderPcoRecordsDatagrid.makeSubmitData = function() {
    var aaoId = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "aaoId"
    });
    var taskId = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "taskId"
    });
    var pid = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "pid"
    });
    var taskKey = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "taskKey"
    });
    var waybillNum = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "waybillNum"
    });
    var operationName = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "operationName"
    });
    var nodeType = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "nodeType"
    });
    var nodeLevel = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "nodeLevel"
    });
    var result = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "result"
    });
    var resultRemark = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "resultRemark"
    });
    var status = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "status"
    });
    var deadline = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "deadline"
    });
    var submitTime = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "submitTime"
    });
    var duration = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "duration"
    });
    var isTimeout = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "isTimeout"
    });
    var timeoutDuration = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "timeoutDuration"
    });
    var responseDeadline = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "responseDeadline"
    });
    var responseTime = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "responseTime"
    });
    var responseDuration = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "responseDuration"
    });
    var isResponseTimeout = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "isResponseTimeout"
    });
    var responseTimeoutDuration = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "responseTimeoutDuration"
    });
    var currentHandler = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "currentHandler"
    });
    var files = CsOrderPcoRecordsDatagrid.grid.datagrid('getEditor', {
        index : CsOrderPcoRecordsDatagrid.editingIndex,
        field : "files"
    });

    var data = {
        aaoId : $(aaoId.target).textbox("getValue"),
        taskId : $(taskId.target).textbox("getValue"),
        pid : $(pid.target).textbox("getValue"),
        taskKey : $(taskKey.target).textbox("getValue"),
        waybillNum : $(waybillNum.target).textbox("getValue"),
        operationName : $(operationName.target).textbox("getValue"),
        nodeType : $(nodeType.target).textbox("getValue"),
        nodeLevel : $(nodeLevel.target).textbox("getValue"),
        result : $(result.target).textbox("getValue"),
        resultRemark : $(resultRemark.target).textbox("getValue"),
        status : $(status.target).textbox("getValue"),
        deadline : $(deadline.target).textbox("getValue"),
        submitTime : $(submitTime.target).textbox("getValue"),
        duration : $(duration.target).textbox("getValue"),
        isTimeout : $(isTimeout.target).textbox("getValue"),
        timeoutDuration : $(timeoutDuration.target).textbox("getValue"),
        responseDeadline : $(responseDeadline.target).textbox("getValue"),
        responseTime : $(responseTime.target).textbox("getValue"),
        responseDuration : $(responseDuration.target).textbox("getValue"),
        isResponseTimeout : $(isResponseTimeout.target).textbox("getValue"),
        responseTimeoutDuration : $(responseTimeoutDuration.target).textbox("getValue"),
        currentHandler : $(currentHandler.target).textbox("getValue"),
        files : $(files.target).textbox("getValue")
    };
    return data;
};

// 清空查询---------------------------------------------------------------------------------------------------
CsOrderPcoRecordsDatagrid.toolbar.find("[tag=clear]").click(function() {
    CsOrderPcoRecordsDatagrid.grid.datagrid("removeFilterRule");
    CsOrderPcoRecordsDatagrid.grid.datagrid("load", {});
});
